<?php
require_once("../includes/config.php");
include DIR_INCLUDES_FT.'bootstrap.php';
header('Content-type: text/html; charset=utf-8');

# array que guarda os ficheiros encontrados na directoria de backup
$files_backup = array();

if ($handle = opendir(DIR_CONTAGENS_BACKUP_FT)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." & $file != ".svn"){
            $files_backup[] = $file;
        }
    }
    closedir($handle);
}

$readable_item = array();

foreach ($files_backup as $file) {
    $query_string = explode(".", $file);
    $contagem = Doctrine_Query::create()->from("Contagens c")->innerJoin("c.Lojas l")->innerJoin("l.Clientes cl")->where("c.ficheiro_backup = ?", $query_string[0])->fetchOne(array(), Doctrine::HYDRATE_ARRAY);
    if ($contagem != 0 && $query_string[1] == 'csv') {
        $readable_item[$query_string[0]] = $contagem;
    }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pt" lang="pt" dir="ltr">
    <head>
        <?require(DIR_FT."header.php");?>
    </head>
    <body>
        <?php require_once(DIR_MENUS_FT."menu.php");?>
        <div id="main_div">
            <form enctype="multipart/form-data" accept-charset="utf-8" id="import_form" action="importar_contagens_edit.php" method="post">
                <fieldset id="form-area">
                    <legend><?=IMPORTAR_CONTAGENS?></legend>
                    <? if(sizeof($readable_item) != 0) {?>
                    <label for="contagem"><?=FICHEIRO?>:</label>
                    <select id="contagem" name="contagem" class="required">
                        <option value=""><?=SELECCIONE_FICHEIRO_BACKUP?></option>
                    <? foreach ($readable_item as $item){?>
                        <option value="<?=$contagem['id']?>"><?=$contagem['Lojas']['Clientes']['nome'] . " - " . $contagem['Lojas']['nome'] . " - " . $contagem['data_fim']?></option>
                    <?}?>
                    </select>
                    <?}?>
                    <label for="ficheiro"><?=FICHEIRO?> Upload:</label>
                    <input type="file" name="ficheiro" id="ficheiro" />
                    <input type="submit" name="submit" value="<?=IMPORTAR?>" class="submit-button" />
                    <div style="clear: both;"></div>
                </fieldset>
            </form>
        </div>
        <?require(DIR_FT."footer.php");?>
    </body>
</html>